package com.daiqee.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.daiqee.controller.support.CommonsController;
import com.daiqee.enums.RequestType;
import com.daiqee.pojo.User;
import com.daiqee.service.impl.UserService;
import com.daiqee.util.MD5Utils;

/**
 * 用户中心,用户相关操作 
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.1
 * @author RichardTang 
 * @date: 2018年5月7日 下午5:57:43
 * @package com.daiqee.handler
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Controller
@RequestMapping("user")
public class UserController extends CommonsController<User>{

	@Autowired
	protected UserService userService;
	
	/**
	 * 处理账号密码修改
	 * 
	 * @param  oldpassword
	 * 						旧密码
	 * @param  newpassword
	 * 						新密码
	 * @param  username
	 * 						需要修改的用户名
	 * 
	 * @return 转发到修改密码页面
	 * 
	 * @throws InterruptedException 
	 */
	@RequestMapping(value = "/pwd",method=RequestMethod.PUT)
	public Object updateAdminPwd(
			@RequestParam(value="oldpassword",required=true) String oldpassword,
			@RequestParam(value="newpassword",required=true)String newpassword,
			@RequestParam(value="username",required=true)String username){
		
		//对老密码进行md5加密操作
		String oldPssword = MD5Utils.md5(oldpassword);
		//对新密码进行md5加密操作
		String newPssword = MD5Utils.md5(newpassword);
		//进行修改密码操作
		Integer result = userService.updatePwdByAdmin(username, oldPssword, newPssword);
		
		return restfulResponse.response(result > 0, RequestType.PUT);
	}
	
	@Override
	public boolean warpperAddMethod(User user) throws Exception {
		//因为前台传送过来的数据并没有携带账号的创建时间,所以这里需要程序来创建
		user.setUserCreateTime(new Date());
		//使用MD5对密码进行加密
		user.setUserPassword(MD5Utils.md5(user.getUserPassword()));
		//调用addService层
		resultNum = userService.addUser(user);
		return resultNum > 0;
	}
	
	@Override
	public boolean warpperUpdateMethod(User user) {
		resultNum = userService.updateUser(user);
		return resultNum > 0;
	}
}
